<template>
  <div class="main">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
    <el-form-item label="提交时间" prop="submitTimeList">
        <el-date-picker
            v-model="queryParams.submitTimeList"
            type="datetimerange"
            range-separator="至"
            format="yyyy-MM-dd HH:mm"
            value-format="yyyy-MM-dd HH:mm"
            start-placeholder="开始日期"
            size="small"
            @keyup.enter.native="handleQuery"
            end-placeholder="结束日期">
        </el-date-picker>
    </el-form-item>
    <el-form-item label="提交人" prop="createBy">
        <el-input
            v-model="queryParams.createBy"
            placeholder="请输入提交人"
            clearable
            size="small"
            style="width:200px"
        />
    </el-form-item>
    <el-form-item label="流程状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
            <el-option
                v-for="item in dict.type.act_status"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="项目名称" prop="projectName">
        <el-select v-model="queryParams.projectName" placeholder="请选择" clearable>
            <el-option
                v-for="item in projectNameInfoList"
                :key="item.value"
                :label="item.projectName"
                :value="item.projectName"
            ></el-option>
        </el-select>
    </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="dept">
        <el-input
          v-model="queryParams.dept"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用类型" prop="costType">
        <el-select v-model="queryParams.costType" placeholder="请选择费用类型" clearable>
          <el-option
            v-for="dict in dict.type.travel_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开户行名称" prop="nameBank" label-width="90px">
        <el-input
          v-model="queryParams.nameBank"
          placeholder="请输入开户行名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡号" prop="cardNumber">
        <el-input
          v-model="queryParams.cardNumber"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
        <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              :height="height"
              :data="financeTravelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="提交时间" align="center" prop="actBusinessStatus.createTime" width="180px" sortable/>
      <el-table-column label="提交人" align="center" prop="actBusinessStatus.createBy" width="180px" sortable/>
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="姓名" align="center" prop="name" sortable/>
      <el-table-column label="部门" align="center" prop="dept" sortable/>
      <el-table-column label="费用类型" align="center" prop="costType" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.travel_type" :value="scope.row.costType"/>
        </template>
      </el-table-column>
      <el-table-column label="开户行名称" align="center" prop="nameBank" sortable/>
      <el-table-column label="卡号" align="center" prop="cardNumber" sortable/>
      <el-table-column label="报销总额" align="center" prop="reimbursementTotal" sortable/>
    <el-table-column label="流程状态" align="center" prop="actBusinessStatus.status" sortable >
        <template slot-scope="scope">
            <dict-tag :options="dict.type.act_status" :value="scope.row.actBusinessStatus.status"/>
        </template>
    </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.actBusinessStatus.status==='draft'||scope.row.actBusinessStatus.status==='back'||scope.row.actBusinessStatus.status==='cancel'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="(scope.row.actBusinessStatus.status==='draft' || scope.row.actBusinessStatus.status==='cancel' || scope.row.actBusinessStatus.status==='back') && (scope.row.createBy === userId || userId === 'admin' || userId === 'sysadmin')"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
            <el-button
                v-if="scope.row.actBusinessStatus.status==='waiting'"
                size="mini"
                type="text"
                icon="el-icon-back"
                @click="cancelProcessApply(scope.row)"
            >撤销</el-button>
            <el-button
                v-if="scope.row.actBusinessStatus.status!=='draft' || scope.row.actBusinessStatus.status==='cancel' || scope.row.actBusinessStatus.status==='back'"
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
            >查看</el-button>
            <el-button
            v-if="scope.row.actBusinessStatus.status!=='draft' || scope.row.actBusinessStatus.status==='cancel' || scope.row.actBusinessStatus.status==='back'"
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="openPrintView(scope.row)"
          >打印</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改差旅费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :before-close="cancel">
    <el-tabs  type="border-card" >
      <el-tab-pane label="业务单据" v-loading="loading">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
              <el-form-item label="公司名称" prop="company">
                  <el-input  v-model="form.company" disabled style="width: 280px;"/>
              </el-form-item>

            <el-form-item label="编号" prop="number">
              <el-input v-model="form.number" disabled />
            </el-form-item>
            <el-form-item label="报销日期" prop="reimbursementDate">
              <el-date-picker clearable
                v-model="form.reimbursementDate"
                type="datetime"
                :disabled='isdisabled'
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择报销日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" :disabled='isdisabled' placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="部门" prop="dept">
              <el-input v-model="form.dept" :disabled='isdisabled' placeholder="请输入部门" />
            </el-form-item>
            <el-form-item label="费用类型" prop="costType">
              <el-select v-model="form.costType"
                         @change="costTypeChange"
                         placeholder="请选择费用类型"
                         :disabled="form.costType&&title!='添加差旅费申请'">
                <el-option
                  v-for="dict in dict.type.travel_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目名称" prop="projectName" v-if="form.costType=='project_pay'">
              <el-select v-model="form.projectName"
                         :disabled="title!='添加差旅费申请'"
                         placeholder="请选择" clearable>
                <el-option
                  v-for="item in projectNameList"
                  :key="item.projectName"
                  :label="item.projectName"
                  :value="item.projectName"
                ></el-option>
              </el-select>
              <!-- <el-input v-model="form.projectName" placeholder="请输入项目名称" :disabled="isDisabled" /> -->
            </el-form-item>
            <div>
              <el-form-item label="出差事由" prop="businessReason" style="width: 100%" class="remarks">
                <el-input :disabled='isdisabled' v-model="form.businessReason" type="textarea" placeholder="请输入内容"/>
              </el-form-item>
            </div>
            <el-form-item label="开户行名称" prop="nameBank">
              <el-input v-model="form.nameBank" :disabled='isdisabled' placeholder="请输入开户行名称" />
            </el-form-item>
            <el-form-item label="卡号" prop="cardNumber">
              <el-input v-model="form.cardNumber" :disabled='isdisabled' placeholder="请输入卡号" />
            </el-form-item>
            <el-form-item label="附件张数" prop="attachmentNumbers">
              <el-input  type="number" v-model="form.attachmentNumbers" :disabled='isdisabled' placeholder="请输入附件张数" />
            </el-form-item>
            <br>
            <el-form-item label="差旅费清单">
              <el-button
                type="primary"
                :disabled="isdisabled"
                icon="el-icon-plus"
                size="mini"
                @click="applayaddRow(applyList)"
              >添加清单</el-button>
            </el-form-item>
            <el-table
              ref="table"
              :data="applyList"
              style="width: 100%;margin-bottom:0.125rem"
              border
            >
              <el-table-column label="序号" align="center" width="55" prop="xh" type="index"></el-table-column>
              <el-table-column prop="departureEvent" label="出发时间" align="center" width="180">
                <template slot-scope="scope">
                  <el-date-picker clearable
                                  v-model="scope.row.departureEvent"
                                  type="datetime"
                                  :disabled='isdisabled'
                                  value-format="yyyy-MM-dd HH:mm:ss"
                                  placeholder="请选择报销日期">
                  </el-date-picker>
                </template>
              </el-table-column>
              <el-table-column prop="departurePlace" label="出发地点" align="center" width="180">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.departurePlace" :disabled="isdisabled"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="strength" label="人数" align="center" width="180">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.strength" :disabled="isdisabled" type="number"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="vehicle" label="交通工具" align="center" width="180">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.vehicle" :disabled="isdisabled"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="transportationFee" label="交通费金额(元)" align="center" width="180">
                <template slot-scope="scope">
                  <el-input
                    @input="scope.row.transportationFee=scope.row.transportationFee.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                    v-model="scope.row.transportationFee"
                            :disabled="isdisabled"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="accommodationDays" label="住宿天数" align="center" width="180">
                <template slot-scope="scope">
                  <el-input type="number" v-model="scope.row.accommodationDays" :disabled="isdisabled"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="accommodationFee" label="住宿费金额(元)" align="center" width="180">
                <template slot-scope="scope">
                  <el-input
                    @input="scope.row.accommodationFee=scope.row.accommodationFee.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                    v-model="scope.row.accommodationFee"
                            :disabled="isdisabled"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="otherName" label="其他费用名称" align="center" width="180">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.otherName" :disabled="isdisabled"></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="otherAmount" label="其他费用金额(元)" align="center" width="180">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.otherAmount"
                            @input="scope.row.otherAmount=scope.row.otherAmount.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                            :disabled="isdisabled"></el-input>
                </template>
              </el-table-column>
              <el-table-column fixed="right" label="操作" align="center">
                <template slot-scope="scope">
                  <el-button
                    @click.native.prevent="applaydeleteRow(scope.$index, applyList)"
                    :disabled="isdisabled"
                    size="small"
                  >移除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-form-item label="交通费总额(元)" prop="transportationFeeTotal" label-width="120px">
              <el-input v-model="form.transportationFeeTotal" disabled placeholder="请输入" />
            </el-form-item>
            <el-form-item label="住宿费总额(元)" prop="accommodationFeeTotal" label-width="120px">
              <el-input v-model="form.accommodationFeeTotal" disabled placeholder="请输入" />
            </el-form-item>
            <el-form-item label="其他费用总额(元)" prop="otherAmountTotal" label-width="130px">
              <el-input v-model="form.otherAmountTotal" disabled placeholder="请输入" />
            </el-form-item>
            <br>
            <el-form-item label="报销总额(人民币大写)" prop="reimbursementTotal" label-width="190px">
              <el-input v-model="form.reimbursementTotal" disabled placeholder="请输入" />
            </el-form-item>
            <el-form-item label="小写" prop="smallLetter">
              <el-input v-model="form.smallLetter" disabled placeholder="请输入" />
            </el-form-item>
            <el-form-item label="预支金额(元)" prop="advanceAmount" label-width="120px">
              <el-input v-model="form.advanceAmount" :disabled='isdisabled' placeholder="请输入预支金额" />
            </el-form-item>
            <el-form-item label="应报销金额(元)" prop="reimbursableAmount" label-width="120px">
              <el-input v-model="form.reimbursableAmount"
                        @input="form.reimbursableAmount=form.reimbursableAmount.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                        :disabled='isdisabled' placeholder="请输入应报销金额" />
            </el-form-item>
            <el-form-item label="应退回金额(元)" prop="refundAmount" label-width="120px">
              <el-input v-model="form.refundAmount"
                        :disabled='isdisabled'
                        @input="form.refundAmount=form.refundAmount.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                        placeholder="请输入应退回金额" />
            </el-form-item>
            <el-form-item label="应付金额(元)" prop="payableAmount" label-width="120px">
              <el-input v-model="form.payableAmount"
                        @input="form.payableAmount=form.payableAmount.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                        :disabled='isdisabled' placeholder="请输入应付金额" />
            </el-form-item>
            <br>
            <el-form-item label="上传附件">
              <file-upload v-model="form.attachment" :is-disabled='isdisabled' :limit="5"/>
            </el-form-item>
          </el-form>
          <div class="dialog-footer" style="float:right" v-if="flag">
            <el-button :loading="buttonLoading" type="info" @click="submitForm('zancun')">暂存</el-button>
            <el-button :loading="buttonLoading" type="primary" @click="submitForm('submit')">提交</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
      </el-tab-pane>
      <!-- 审批历史 -->
      <el-tab-pane label="审批历史" v-if="processInstanceId">
            <history ref="historyRef" :processInstanceId="processInstanceId" ></history>
      </el-tab-pane>
    </el-tabs>
    </el-dialog>
    <printForm
      ref="printForm"
      form-type="差旅费申请"
      :form="form"
      :history="history"
      :printObject="printObject"
      :actBusinessStatus="actBusinessStatus"
    ></printForm>
      <!-- 提交 -->
      <verify ref="verifyRef"
              @submitCallback="submitCallback"
      :componentName="componentName"
              :taskId="taskId"
              :deptId="dept.deptId"
              :taskVariables="taskVariables"
              :sendMessage="sendMessage"></verify>
  </div>
</template>

<script>
import printForm from "@/views/components/printForm.vue";
import { listByIds } from "@/api/system/oss";
import { getDept } from "@/api/system/role";
import { listFinanceTravel, getFinanceTravel, delFinanceTravel, addFinanceTravel, updateFinanceTravel,getProjectList } from "@/api/oa/financeTravel";
import processApi from "@/api/workflow/processInst";
import history from "@/components/Process/History";
import verify from "@/components/Process/Verify";
import {parseTime} from "@/utils/ruoyi";
export default {
  name: "FinanceTravel",
  dicts: ['travel_type','act_status'],
  components: {
      history,
      verify,
    printForm
  },
props:{
    projectNameInfoList:Array,
    projectNameByList:String,
},
  data() {
    return {
      componentName: "差旅费报销申请",
      printObject: [],
      history: [],
      actBusinessStatus: {},
      height:`calc(100% - 88px)`,
      applyList:[{}],
      projectNameList: [],
      isdisabled:false,
      // 流程实例
      processInstanceId: '',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 差旅费表格数据
      financeTravelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        projectName:undefined,
        pageSize: 10,
        submitTimeList: [],
        createBy:'',
        status:'',
        name: undefined,
        dept: undefined,
        costType: undefined,
        nameBank: undefined,
        cardNumber: undefined,
        reimbursementTotal: undefined,
      },
      // 表单参数
      form: {
        company:"江苏华琛能源发展有限公司",
      },
      // 表单校验
      rules: {
        reimbursementDate: [
          { required: true, message: "报销日期不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        dept: [
          { required: true, message: "部门不能为空", trigger: "blur" }
        ],
        businessReason: [
          { required: true, message: "出差事由不能为空", trigger: "blur" }
        ],
        costType: [
          { required: true, message: "费用类型不能为空", trigger: "change" }
        ],
        nameBank: [
          { required: true, message: "开户行名称不能为空", trigger: "blur" }
        ],
        cardNumber: [
          { required: true, message: "卡号不能为空", trigger: "blur" }
        ],
        attachmentNumbers: [
          { required: true, message: "附件张数不能为空", trigger: "blur" }
        ],
        reimbursementTotal: [
          { required: true, message: "报销总额不能为空", trigger: "blur" }
        ],
        smallLetter: [
          { required: true, message: "小写不能为空", trigger: "blur" }
        ],
        advanceAmount: [
          { required: true, message: "预支金额不能为空", trigger: "blur" }
        ],
        reimbursableAmount: [
          { required: true, message: "应报销金额不能为空", trigger: "blur" }
        ],
        refundAmount: [
          { required: true, message: "应退回金额不能为空", trigger: "blur" }
        ],
        payableAmount: [
          { required: true, message: "应付金额不能为空", trigger: "blur" }
        ],
      },
      taskVariables: {}, //流程变量
      taskId: undefined, //任务id
      flag: true,
      // 消息提醒
      sendMessage: {},
      processKey: 'oa_finance_travel',
      userId:'',
      dept:"",
      isManager:false,
      deptId:"",
    };
  },
  created() {
  },
  mounted() {
      this.$nextTick(() => {
        if (this.projectNameByList){
          this.handleAdd();
          this.form.costType="project_pay";
          this.form.projectName=this.projectNameByList;
          this.$emit('deleteProjectName');
        }
        this.getList();
        let formHeight = this.$refs.queryForm.$el.offsetHeight;
        this.height = `calc(100% - ${formHeight}px - 88px)`;
      },300)
    window.onresize = () => {
      let formHeight = this.$refs.queryForm.$el.offsetHeight;
      this.height = `calc(100% - ${formHeight}px - 88px)`;
    };
      this.userId=this.$store.state.user.name;
    let roles=this.$store.state.user.roles
    if (roles.indexOf('common')!=-1||roles.indexOf('accountant')!=-1||
      roles.indexOf('cashier')!=-1){
      this.isManager=false;
    }else {
      this.isManager=true;
    }
    if (this.$store.state.user.dept) {
      this.deptId = this.userId = this.$store.state.user.dept.deptId;
    }
  },
  watch:{
    applyList:{
      handler(val) {
        let transportationFeeTotal = 0
        let accommodationFeeTotal = 0
        let otherAmountTotal = 0
        for (let i = 0; i < val.length; i++) {
          transportationFeeTotal += parseFloat(val[i].transportationFee) || 0
          accommodationFeeTotal += parseFloat(val[i].accommodationFee) || 0
          otherAmountTotal += parseFloat(val[i].otherAmount) || 0
        }
        // 将计算结果赋值给对应的变量
        this.form.transportationFeeTotal = transportationFeeTotal.toFixed(2)
        this.form.accommodationFeeTotal = accommodationFeeTotal.toFixed(2)
        this.form.otherAmountTotal = otherAmountTotal.toFixed(2)
        // 计算报销总额
        this.form.smallLetter = (transportationFeeTotal + accommodationFeeTotal + otherAmountTotal).toFixed(2)
        this.form.reimbursementTotal=this.digitUppercase(this.form.smallLetter)
      },
      deep: true, // 可以深度检测到 obj 对象的属性值的变化
      immediate: true //刷新加载  立马触发一次handler
    }
  },
  methods: {
    async openPrintView(row) {
      const { data } = await getFinanceTravel(row.id);
      this.form = data;
      this.form.company="江苏华琛能源发展有限公司";
      this.applyList = JSON.parse(data.applyList);
      this.actBusinessStatus = data.actBusinessStatus;
      console.log(this.actBusinessStatus.createBy);
      const res = await getDept(this.actBusinessStatus.createBy);
      this.actBusinessStatus.deptName = res.data ? res.data.deptName : "";
      const res2 = await processApi.getHistoryInfoList(
        this.actBusinessStatus.processInstanceId
      );
      this.history = res2.data.reverse();
      let cost_type = this.dict.type.travel_type.filter(item2 => {
          return this.form.costType == item2.value;
        })[0];
      this.form.costTypeLabel = cost_type.label;
      let attachments = [];
      if(this.form.attachment){
        const res3 = await listByIds(this.form.attachment)
        attachments = res3.data.map(item=>{
          return item.originalName
        })
      }
      // this.printView = true;
      this.printObject = [
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "公司名称"
            },
            {
              colspan: 7,
              align: "left",
              label: this.form.company
            }
          ]
        },
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "报销日期"
            },
            {
              colspan: 7,
              align: "left",
              label: this.form.reimbursementDate
            }
          ]
        },
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "姓名"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.name
            },
            {
              colspan: 1,
              align: "center",
              label: "部门"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.dept
            }
          ]
        },
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "出差事由"
            },
            {
              colspan: 7,
              align: "left",
              label: this.form.businessReason
            }
          ]
        },
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "费用类型"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.costTypeLabel
            },
            {
              colspan: 1,
              align: "center",
              label: "项目名称"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.costTypeLabel=='项目费用'?this.form.projectName:'无'
            }
          ]
        },

        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "开户行名称"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.nameBank
            },
            {
              colspan: 1,
              align: "center",
              label: "卡号"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.cardNumber
            }
          ]
        },
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "附件张数"
            },
            {
              colspan: 7,
              align: "left",
              label: this.form.attachmentNumbers
            }
          ]
        },
        {
          tds: [
            {
              colspan: 8,
              align: "center",
              label: "差旅费明细"
            }
          ]
        },
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "出发时间"
            },
            {
              colspan: 1,
              align: "center",
              label: "出发地点"
            },
            {
              colspan: 1,
              align: "center",
              label: "人数"
            },
            {
              colspan: 1,
              align: "center",
              label: "交通工具/金额(元)"
            },
            {
              colspan: 1,
              align: "center",
              label: "住宿天数/金额(元)"
            },
            {
              colspan: 3,
              align: "center",
              label: "其他费用名称/其他费用金额(元)"
            }
          ]
        }
      ];
      let list = this.applyList.map(item => {
        return {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: item.departureEvent
            },
            {
              colspan: 1,
              align: "center",
              label: item.departurePlace
            },
            {
              colspan: 1,
              align: "center",
              label: item.strength
            },
            {
              colspan: 1,
              align: "center",
              label: item.vehicle +" / " + item.transportationFee + "元"
            },
            {
              colspan: 1,
              align: "center",
              label: item.accommodationDays +" / " + item.accommodationFee + "元"
            },
            {
              colspan: 3,
              align: "center",
              label: item.otherName +" / " + item.otherAmount + "元"
            }
          ]
        };
      });
      this.printObject = this.printObject.concat(list);
      this.printObject.push({
        tds: [
          {
            colspan: 1,
            align: "center",
            label: "交通费总额(元)"
          },
          {
            colspan: 3,
            align: "left",
            label: this.form.transportationFeeTotal + "元"
          },
          {
            colspan: 1,
            align: "center",
            label: "住宿费总额(元)"
          },
          {
            colspan: 3,
            align: "left",
            label: this.form.accommodationFeeTotal + "元"
          }
        ]
      });
      this.printObject.push(
        {
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "其他费用总额(元)"
            },
            {
              colspan: 7,
              align: "left",
              label: this.form.otherAmountTotal + "元"
            }
          ]
        }
      )
      this.printObject.push({
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "报销总额(元)"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.smallLetter + "元"
            },
            {
              colspan: 1,
              align: "center",
              label: "报销总额(大写)"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.reimbursementTotal + "元"
            }
          ]
        })
        this.printObject.push({
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "预支金额(元)"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.advanceAmount + "元"
            },
            {
              colspan: 1,
              align: "center",
              label: "应报销金额(元)"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.reimbursableAmount + "元"
            }
          ]
        })
        this.printObject.push({
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "应退回金额(元)"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.refundAmount + "元"
            },
            {
              colspan: 1,
              align: "center",
              label: "应付金额(元)"
            },
            {
              colspan: 3,
              align: "left",
              label: this.form.payableAmount + "元"
            }
          ]
        })
        this.printObject.push({
          tds: [
            {
              colspan: 1,
              align: "center",
              label: "附件"
            },
            {
              colspan: 7,
              align: "left",
              label: attachments
            },
          ]
        })
      this.$refs.printForm.open();
    },
    /** 查询差旅费列表 */
    getList() {
      this.loading = true;
      listFinanceTravel(this.queryParams).then(response => {
        this.financeTravelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
        this.getProjectList();
    },
    // 取消按钮
    cancel() {
      this.isdisabled=false
      this.open = false;
      this.applyList = [{}];
      this.reset();
    },
    costTypeChange(){
      this.form.projectName=undefined;
      this.getProjectList();
    },
    // 表单重置
    reset() {
      this.form = {
        company:"江苏华琛能源发展有限公司",
        id: undefined,
        deptId: undefined,
        userId: undefined,
        projectName: undefined,
        reimbursementDate: undefined,
        number: undefined,
        name: undefined,
        dept: undefined,
        businessReason: undefined,
        costType: undefined,
        nameBank: undefined,
        cardNumber: undefined,
        attachmentNumbers: undefined,
        accommodationFeeTotal:undefined,
        otherAmountTotal:undefined,
        transportationFeeTotal:undefined,
        reimbursementTotal: undefined,
        smallLetter: undefined,
        advanceAmount: undefined,
        reimbursableAmount: undefined,
        refundAmount: undefined,
        payableAmount: undefined,
        createTime: undefined,
        updateTime: undefined,
        createBy: undefined,
        updateBy: undefined,
        attachment: undefined
      };
      this.processInstanceId = undefined
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
       this.resetForm("queryForm");
      this.handleQuery();
    },
    applayaddRow(tableData1) {
      tableData1.push({
        departureEvent:"",
        departurePlace:"",
        strength: "",
        vehicle:"",
        transportationFee:"",
        accommodationDays:"",
        accommodationFee:"",
        otherName:"",
        otherAmount:"",
        xh: this.xh1,
        infoId: this.infoId
      });
    },
    // 金额数字转大写
    digitUppercase(price) {
      const fraction = ["角", "分"];
      const digit = [
        "零",
        "壹",
        "贰",
        "叁",
        "肆",
        "伍",
        "陆",
        "柒",
        "捌",
        "玖"
      ];
      const unit = [
        ["元", "万", "亿"],
        ["", "拾", "佰", "仟"]
      ];
      let num = Math.abs(price);
      let s = "";
      fraction.forEach((item, index) => {
        s += (digit[Math.floor(num * 10 * 10 ** index) % 10] + item).replace(
          /零./,
          ""
        );
      });
      s = s || "整";
      num = Math.floor(num);
      for (let i = 0; i < unit[0].length && num > 0; i += 1) {
        let p = "";
        for (let j = 0; j < unit[1].length && num > 0; j += 1) {
          p = digit[num % 10] + unit[1][j] + p;
          num = Math.floor(num / 10);
        }
        s = p.replace(/(零.)*零$/, "").replace(/^$/, "零") + unit[0][i] + s;
      }

      return s
        .replace(/(零.)*零元/, "元")
        .replace(/(零.)+/g, "零")
        .replace(/^整$/, "零元整");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
      if (selection.length==1){
          if (selection[0].actBusinessStatus.status=='draft'||selection[0].actBusinessStatus.status=='back'||selection[0].actBusinessStatus.status=='cancel'){
             }else {
              this.single=true;
          }
      }
    },
    //删除表格里的行(申请状态)
    applaydeleteRow(index, rows) {
      rows.splice(index, 1);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.flag = true
      this.reset();
      this.open = true;
      this.title = "添加差旅费申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.flag = true
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getFinanceTravel(id).then(response => {
        this.applyList = JSON.parse(response.data.applyList);
        this.loading = false;
        this.form = response.data;
        this.form.company="江苏华琛能源发展有限公司";
        if (response.data.actBusinessStatus.createTime){
          this.form.number=this.processKey + '@' + parseTime(response.data.actBusinessStatus.createTime, '{y}{m}{d}{h}{i}{s}')
        }
        this.$nextTick(() => {
            this.processInstanceId=response.data.actBusinessStatus.processInstanceId
        })
        this.open = true;
        this.title = "修改差旅费申请";
      });
    },
  getProjectList() {
      getProjectList().then(res => {
          this.projectNameList = res.data;
      });
  },
    //查看
    handleView(row){
        this.flag = false
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getFinanceTravel(id).then(response => {
          this.applyList = JSON.parse(response.data.applyList);
          this.loading = false;
          this.form = response.data;
          this.form.company="江苏华琛能源发展有限公司";
          if (response.data.actBusinessStatus.createTime){
            this.form.number=this.processKey + '@' + parseTime(response.data.actBusinessStatus.createTime, '{y}{m}{d}{h}{i}{s}')
          }
          this.$nextTick(() => {
              this.processInstanceId=response.data.actBusinessStatus.processInstanceId
          })
          this.isdisabled=true
          this.open = true;
          this.title = "查看差旅费业务";
        });
    },
    /** 提交按钮 */
    submitForm(flag) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.applyList = JSON.stringify(this.applyList);
          if (this.form.id != null) {
            updateFinanceTravel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              if(flag === 'submit'){
                  this.submitFormApply(response.data)
              }else {
                  this.open = false;
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addFinanceTravel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              if(flag === 'submit'){
                this.submitFormApply(response.data)
              }else {
                this.open = false;
                this.cancel();
              }
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    // 提交成功回调
    submitCallback(){
        this.open = false;
        this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除差旅费编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delFinanceTravel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/financeTravel/export', {
        ...this.queryParams
      }, `financeTravel_${new Date().getTime()}.xlsx`)
    },
    //提交流程
    submitFormApply(entity){
        //流程变量
        this.taskVariables = {
            entity: entity,
          isManager:this.isManager,
          deptId:this.deptId,
            userRole: this.$store.state.user.roles[0],
            reimbursementTotal:Math.round(this.form.smallLetter*100)/100
        }
        const data = {
            processKey: this.processKey, // key
            businessKey: entity.id, // 业务id
            variables: this.taskVariables,
            classFullName: 'com.ruoyi.oa.domain.OaPurchaseParity'
        }
        this.sendMessage = {
					title: "差旅费报销申请",
					messageContent: this.$store.state.user.nickName + '提交了差旅费报销申请'
				}
        // 启动流程
        processApi.startProcessApply(data).then(response => {
            this.taskId = response.data.taskId;
            this.$refs.verifyRef.visible = true
            this.$refs.verifyRef.reset()
        })
    },
    //撤回
    cancelProcessApply(row){
        this.$modal.confirm('是否撤销申请').then(() => {
        this.loading = true;
        return processApi.cancelProcessApply(row.actBusinessStatus.processInstanceId);
        }).then(() => {
        this.getList();
        this.$modal.msgSuccess("撤回成功");
        }).finally(() => {
        this.loading = false;
        });
    },
  }
};
</script>
<style scoped>
.main{
  height: 100%;
}
    /* 设置滚动条宽度和高度 */
    ::v-deep .el-table__body-wrapper::-webkit-scrollbar {
        width: 8px; /* 横向滚动条 */
        height: 8px; /* 纵向滚动条 必写 */
    }
    /* 设置滚动条样式 */
    ::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
        background-color: #dde;
        border-radius: 3px;
    }
    #selectForm >>> .el-form-item__label {
        font-size: 12px;
    }
    ::v-deep .bjsm .el-form-item__content{
        width: calc(100% - 130px) !important;
    }

    ::v-deep.remarks .el-form-item__content {
  width: 75% !important;
}
</style>
